home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / undo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-04  |  15.1 KB  |  554 lines

  1. /* undo handling for XEmacs.
  2.    Copyright (C) 1990, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: FSF 19.28. */
  21.  
  22. /* This file has been Mule-ized. */
  23.  
  24. #include <config.h>
  25. #include "lisp.h"
  26. #include "buffer.h"
  27. #include "extents.h"
  28.  
  29. /* Maintained in event-stream.c */
  30. extern Bufpos last_point_position;
  31. extern Lisp_Object last_point_position_buffer;
  32.  
  33. /* Extent code needs to know about undo because the behavior of insert()
  34.    with regard to extents varies depending on whether we are inside
  35.    an undo or not. */
  36. int inside_undo;
  37.  
  38. /* Last buffer for which undo information was recorded.  */
  39. static Lisp_Object last_undo_buffer;
  40.  
  41. Lisp_Object Qinhibit_read_only;
  42.  
  43. /* The first time a command records something for undo.
  44.    it also allocates the undo-boundary object
  45.    which will be added to the list at the end of the command.
  46.    This ensures we can't run out of space while trying to make
  47.    an undo-boundary.  */
  48. Lisp_Object pending_boundary;
  49.  
  50. static void
  51. undo_boundary (struct buffer *b)
  52. {
  53.   Lisp_Object tem = Fcar (b->undo_list);
  54.   if (!NILP (tem))
  55.     {
  56.       /* One way or another, cons nil onto the front of the undo list.  */
  57.       if (CONSP (pending_boundary))
  58.     {
  59.       /* If we have preallocated the cons cell to use here,
  60.          use that one.  */
  61.       XCDR (pending_boundary) = b->undo_list;
  62.       b->undo_list = pending_boundary;
  63.       pending_boundary = Qnil;
  64.     }
  65.       else
  66.     b->undo_list = Fcons (Qnil, b->undo_list);
  67.     }
  68. }
  69.  
  70.  
  71. static int
  72. undo_prelude (struct buffer *b, int hack_pending_boundary)
  73. {
  74.   if (EQ (b->undo_list, Qt))
  75.     return (0);
  76.  
  77.   if (NILP (last_undo_buffer) || b != XBUFFER (last_undo_buffer))
  78.   {
  79.     undo_boundary (b);
  80.     XSETBUFFER (last_undo_buffer, b);
  81.   }
  82.   
  83.   /* Allocate a cons cell to be the undo boundary after this command.  */
  84.   if (hack_pending_boundary && NILP (pending_boundary))
  85.     pending_boundary = Fcons (Qnil, Qnil);
  86.  
  87.   if (BUF_MODIFF (b) <= b->save_modified)
  88.   {
  89.     /* Record that an unmodified buffer is about to be changed.
  90.        Record the file modification date so that when undoing this entry
  91.        we can tell whether it is obsolete because the file was saved again.  */
  92.     b->undo_list
  93.       = Fcons (Fcons (Qt,
  94.                       Fcons (make_number ((b->modtime >> 16) & 0xffff),
  95.                              make_number (b->modtime & 0xffff))),
  96.                b->undo_list);
  97.   }
  98.   return (1);
  99. }
  100.  
  101.  
  102.  
  103. static Lisp_Object
  104. restore_inside_undo (Lisp_Object val)
  105. {
  106.   inside_undo = XINT (val);
  107.   return val;
  108. }
  109.  
  110.  
  111. /* Record an insertion that just happened or is about to happen,
  112.    for LENGTH characters at position BEG.
  113.    (It is possible to record an insertion before or after the fact
  114.    because we don't need to record the contents.)  */
  115.  
  116. void
  117. record_insert (struct buffer *b, Bufpos beg, Charcount length)
  118. {
  119.   if (!undo_prelude (b, 1))
  120.     return;
  121.  
  122.   /* If this is following another insertion and consecutive with it
  123.      in the buffer, combine the two.  */
  124.   if (CONSP (b->undo_list))
  125.     {
  126.       Lisp_Object elt;
  127.       elt = XCAR (b->undo_list);
  128.       if (CONSP (elt)
  129.       && INTP (XCAR (elt))
  130.       && INTP (XCDR (elt))
  131.       && XINT (XCDR (elt)) == beg)
  132.     {
  133.       XCDR (elt) = make_number (beg + length);
  134.       return;
  135.     }
  136.     }
  137.  
  138.   b->undo_list = Fcons (Fcons (make_number (beg), 
  139.                                make_number (beg + length)),
  140.                         b->undo_list);
  141. }
  142.  
  143. /* Record that a deletion is about to take place,
  144.    for LENGTH characters at location BEG.  */
  145.  
  146. #ifdef ENERGIZE
  147. extern int inside_parse_buffer; /* total kludge */
  148. #endif
  149.  
  150. void
  151. record_delete (struct buffer *b, Bufpos beg, Charcount length)
  152. {
  153.   /* This function can GC */
  154.   Lisp_Object sbeg;
  155.   int at_boundary;
  156.  
  157. #ifdef ENERGIZE
  158.   /* #### why is this necessary? */
  159.   if (inside_parse_buffer)
  160.     return;
  161. #endif
  162.  
  163.   if (!undo_prelude (b, 1))
  164.     return;
  165.  
  166.   at_boundary = (CONSP (b->undo_list)
  167.          && NILP (XCAR (b->undo_list)));
  168.  
  169.   if (BUF_PT (b) == beg + length)
  170.     sbeg = make_number (-beg);
  171.   else
  172.     sbeg = make_number (beg);
  173.  
  174.   /* If we are just after an undo boundary, and 
  175.      point wasn't at start of deleted range, record where it was.  */
  176.   if (at_boundary
  177.       && BUFFERP (last_point_position_buffer)
  178.       && b == XBUFFER (last_point_position_buffer)
  179.       && last_point_position != XINT (sbeg))
  180.     b->undo_list = Fcons (make_number (last_point_position), b->undo_list);
  181.  
  182.   b->undo_list = Fcons (Fcons (make_string_from_buffer (b, beg,
  183.                             length),
  184.                                sbeg),
  185.                         b->undo_list);
  186. }
  187.  
  188. /* Record that a replacement is about to take place,
  189.    for LENGTH characters at location BEG.
  190.    The replacement does not change the number of characters.  */
  191.  
  192. void
  193. record_change (struct buffer *b, Bufpos beg, Charcount length)
  194. {
  195.   record_delete (b, beg, length);
  196.   record_insert (b, beg, length);
  197. }
  198.  
  199. /* Record that an EXTENT is about to be attached or detached in its buffer.
  200.    This works much like a deletion or insertion, except that there's no string.
  201.    The tricky part is that the buffer we operate on comes from EXTENT.
  202.    Most extent changes happen as a side effect of string insertion and
  203.    deletion; this call is solely for Fdetach_extent() and Finsert_extent().
  204.    */
  205. void
  206. record_extent (Lisp_Object extent, int attached)
  207. {
  208.   Lisp_Object buffer = Fextent_object (extent);
  209.   struct buffer *b = XBUFFER (buffer); /* !!#### */
  210.   Lisp_Object token;
  211.  
  212.   if (!undo_prelude (b, 1))
  213.     return;
  214.  
  215.   if (attached)
  216.     token = extent;
  217.   else
  218.     {
  219.       Lisp_Object xbeg = Fextent_start_position (extent);
  220.       Lisp_Object xend = Fextent_end_position (extent);
  221.       XSETEXTENT_REPLICA (token,
  222.            make_extent_replica (extent, XINT (xbeg), XINT (xend)));
  223.     }
  224.   b->undo_list = Fcons (token, b->undo_list);
  225. }
  226.  
  227. #if 0 /* FSFmacs */
  228. /* Record a change in property PROP (whose old value was VAL)
  229.    for LENGTH characters starting at position BEG in BUFFER.  */
  230.  
  231. record_property_change (Bufpos beg, Charcount length,
  232.                         Lisp_Object prop, Lisp_Object value,
  233.                         Lisp_Object buffer)
  234. {
  235.   Lisp_Object lbeg, lend, entry;
  236.   struct buffer *b = XBUFFER (buffer);
  237.  
  238.   if (!undo_prelude (b, 1))
  239.     return;
  240.  
  241.   lbeg = make_number (beg);
  242.   lend = make_number (beg + length);
  243.   entry = Fcons (Qnil, Fcons (prop, Fcons (value, Fcons (lbeg, lend))));
  244.   b->undo_list = Fcons (entry, b->undo_list);
  245. }
  246. #endif /* FSFmacs */
  247.  
  248.  
  249. DEFUN ("undo-boundary", Fundo_boundary, Sundo_boundary, 0, 0, 0,
  250.   "Mark a boundary between units of undo.\n\
  251. An undo command will stop at this point,\n\
  252. but another undo command will undo to the previous boundary.")
  253.   ()
  254. {
  255.   if (EQ (current_buffer->undo_list, Qt))
  256.     return Qnil;
  257.   undo_boundary (current_buffer);
  258.   return Qnil;
  259. }
  260.  
  261. /* At garbage collection time, make an undo list shorter at the end,
  262.    returning the truncated list.
  263.    MINSIZE and MAXSIZE are the limits on size allowed, as described below.
  264.    In practice, these are the values of undo-threshold and
  265.    undo-high-threshold.  */
  266.  
  267. Lisp_Object
  268. truncate_undo_list (Lisp_Object list, int minsize, int maxsize)
  269. {
  270.   Lisp_Object prev, next, last_boundary;
  271.   int size_so_far = 0;
  272.  
  273.   if (!(minsize > 0 || maxsize > 0))
  274.     return list;
  275.  
  276.   prev = Qnil;
  277.   next = list;
  278.   last_boundary = Qnil;
  279.  
  280.   if (!CONSP (list))
  281.     return (list);
  282.  
  283.   /* Always preserve at least the most recent undo record.
  284.      If the first element is an undo boundary, skip past it. */
  285.   if (CONSP (next)
  286.       && NILP (XCAR (next)))
  287.     {
  288.       /* Add in the space occupied by this element and its chain link.  */
  289.       size_so_far += sizeof (struct Lisp_Cons);
  290.  
  291.       /* Advance to next element.  */
  292.       prev = next;
  293.       next = XCDR (next);
  294.     }
  295.   while (CONSP (next)
  296.      && !NILP (XCAR (next)))
  297.     {
  298.       Lisp_Object elt;
  299.       elt = XCAR (next);
  300.  
  301.       /* Add in the space occupied by this element and its chain link.  */
  302.       size_so_far += sizeof (struct Lisp_Cons);
  303.       if (CONSP (elt))
  304.     {
  305.       size_so_far += sizeof (struct Lisp_Cons);
  306.       if (STRINGP (XCAR (elt)))
  307.         size_so_far += (sizeof (struct Lisp_String) - 1
  308.                 + string_length (XSTRING (XCAR (elt))));
  309.     }
  310.  
  311.       /* Advance to next element.  */
  312.       prev = next;
  313.       next = XCDR (next);
  314.     }
  315.   if (CONSP (next))
  316.     last_boundary = prev;
  317.  
  318.   while (CONSP (next))
  319.     {
  320.       Lisp_Object elt;
  321.       elt = XCAR (next);
  322.  
  323.       /* When we get to a boundary, decide whether to truncate
  324.      either before or after it.  The lower threshold, MINSIZE,
  325.      tells us to truncate after it.  If its size pushes past
  326.      the higher threshold MAXSIZE as well, we truncate before it.  */
  327.       if (NILP (elt))
  328.     {
  329.       if (size_so_far > maxsize && maxsize > 0)
  330.         break;
  331.       last_boundary = prev;
  332.       if (size_so_far > minsize && minsize > 0)
  333.         break;
  334.     }
  335.  
  336.       /* Add in the space occupied by this element and its chain link.  */
  337.       size_so_far += sizeof (struct Lisp_Cons);
  338.       if (CONSP (elt))
  339.     {
  340.       size_so_far += sizeof (struct Lisp_Cons);
  341.       if (STRINGP (XCAR (elt)))
  342.         size_so_far += (sizeof (struct Lisp_String) - 1
  343.                             + string_length (XSTRING (XCAR (elt))));
  344.     }
  345.  
  346.       /* Advance to next element.  */
  347.       prev = next;
  348.       next = XCDR (next);
  349.     }
  350.  
  351.   /* If we scanned the whole list, it is short enough; don't change it.  */
  352.   if (NILP (next))
  353.     return list;
  354.  
  355.   /* Truncate at the boundary where we decided to truncate.  */
  356.   if (!NILP (last_boundary))
  357.     {
  358.       XCDR (last_boundary) = Qnil;
  359.       return list;
  360.     }
  361.   else
  362.     return Qnil;
  363. }
  364.  
  365. DEFUN ("primitive-undo", Fprimitive_undo, Sprimitive_undo, 2, 2, 0,
  366.   "Undo COUNT records from the front of the list LIST.\n\
  367. Return what remains of the list.")
  368.   (count, list)
  369.      Lisp_Object count, list;
  370. {
  371.   struct gcpro gcpro1, gcpro2;
  372.   Lisp_Object next = Qnil;
  373.   /* This function can GC */
  374.   int arg;
  375.   int speccount = specpdl_depth ();
  376.  
  377.   record_unwind_protect (restore_inside_undo, make_number (inside_undo));
  378.   inside_undo = 1;
  379.  
  380. #if 0  /* This is a good feature, but would make undo-start
  381.       unable to do what is expected.  */
  382.   Lisp_Object tem;
  383.  
  384.   /* If the head of the list is a boundary, it is the boundary
  385.      preceding this command.  Get rid of it and don't count it.  */
  386.   tem = Fcar (list);
  387.   if (NILP (tem))
  388.     list = Fcdr (list);
  389. #endif
  390.  
  391.   CHECK_INT (count, 0);
  392.   arg = XINT (count);
  393.   next = Qnil;
  394.   GCPRO2 (next, list);
  395.  
  396.   /* Don't let read-only properties interfere with undo.  */
  397.   if (NILP (current_buffer->read_only))
  398.     specbind (Qinhibit_read_only, Qt);
  399.  
  400.   while (arg > 0)
  401.     {
  402.       while (1)
  403.     {
  404.           if (NILP (list))
  405.             break;
  406.           else if (!CONSP (list))
  407.             goto rotten;
  408.       next = XCAR (list);
  409.       list = XCDR (list);
  410.       /* Exit inner loop at undo boundary.  */
  411.       if (NILP (next))
  412.         break;
  413.       /* Handle an integer by setting point to that value.  */
  414.       else if (INTP (next))
  415.         BUF_SET_PT (current_buffer, bufpos_clip_to_bounds (BUF_BEGV (current_buffer), XINT (next), BUF_ZV (current_buffer)));
  416.       else if (CONSP (next))
  417.         {
  418.           Lisp_Object car = XCAR (next);
  419.               Lisp_Object cdr = XCDR (next);
  420.  
  421.               if (EQ (car, Qt))
  422.         {
  423.           /* Element (t high . low) records previous modtime.  */
  424.           Lisp_Object high, low;
  425.           int mod_time;
  426.           if (!CONSP (cdr)) goto rotten;
  427.           high = XCAR (cdr);
  428.           low = XCDR (cdr);
  429.           if (!INTP (high) || !INTP (low)) goto rotten;
  430.           mod_time = (XINT (high) << 16) + XINT (low);
  431.           /* If this records an obsolete save
  432.              (not matching the actual disk file)
  433.              then don't mark unmodified.  */
  434.           if (mod_time != current_buffer->modtime)
  435.             break;
  436. #ifdef CLASH_DETECTION
  437.           Funlock_buffer ();
  438. #endif /* CLASH_DETECTION */
  439.           Fset_buffer_modified_p (Qnil, Fcurrent_buffer ()); /* may GC under ENERGIZE */
  440.         }
  441. #if 0 /* FSFmacs */
  442.           else if (EQ (car, Qnil))
  443.         {
  444.           /* Element (nil prop val beg . end) is property change.  */
  445.           Lisp_Object beg, end, prop, val;
  446.  
  447.           prop = Fcar (cdr);
  448.           cdr = Fcdr (cdr);
  449.           val = Fcar (cdr);
  450.           cdr = Fcdr (cdr);
  451.           beg = Fcar (cdr);
  452.           end = Fcdr (cdr);
  453.  
  454.           Fput_text_property (beg, end, prop, val, Qnil);
  455.         }
  456. #endif /* FSFmacs */
  457.           else if (INTP (car) && INTP (cdr))
  458.         {
  459.           /* Element (BEG . END) means range was inserted.  */
  460.  
  461.           if (XINT (car) < BUF_BEGV (current_buffer)
  462.               || XINT (cdr) > BUF_ZV (current_buffer))
  463.             error ("Changes to be undone are outside visible portion of buffer");
  464.           /* Set point first thing, so that undoing this undo
  465.              does not send point back to where it is now.  */
  466.           Fgoto_char (car, Fcurrent_buffer ());
  467.           Fdelete_region (car, cdr, Fcurrent_buffer ());
  468.         }
  469.           else if (STRINGP (car) && INTP (cdr))
  470.         {
  471.           /* Element (STRING . POS) means STRING was deleted.  */
  472.           Lisp_Object membuf = car;
  473.           int pos = XINT (cdr);
  474.  
  475.           if (pos < 0)
  476.             {
  477.               if (-pos < BUF_BEGV (current_buffer) || -pos > BUF_ZV (current_buffer))
  478.             error ("Changes to be undone are outside visible portion of buffer");
  479.               BUF_SET_PT (current_buffer, -pos);
  480.               Finsert (1, &membuf);
  481.             }
  482.           else
  483.             {
  484.               if (pos < BUF_BEGV (current_buffer) || pos > BUF_ZV (current_buffer))
  485.             error ("Changes to be undone are outside visible portion of buffer");
  486.               BUF_SET_PT (current_buffer, pos);
  487.  
  488.               /* Insert before markers so that if the mark is
  489.              currently on the boundary of this deletion, it
  490.              ends up on the other side of the now-undeleted
  491.              text from point.  Since undo doesn't even keep
  492.              track of the mark, this isn't really necessary,
  493.              but it may lead to better behavior in certain
  494.              situations.
  495.              
  496.              I'm doubtful that this is safe; you could mess
  497.              up the process-output mark in shell buffers, so
  498.              until I hear a compelling reason for this change,
  499.              I'm leaving it out. -jwz
  500.              */
  501.               /* Finsert_before_markers (1, &membuf); */
  502.               Finsert (1, &membuf);
  503.               BUF_SET_PT (current_buffer, pos);
  504.             }
  505.         }
  506.           else
  507.         {
  508.           goto rotten;
  509.         }
  510.         }
  511.       else if (EXTENTP (next))
  512.         Fdetach_extent (next);
  513.       else if (EXTENT_REPLICAP (next))
  514.         {
  515.            Lisp_Object extent_obj, start, end;
  516.  
  517.            extent_obj = Fextent_replica_extent (next);
  518.            start = Fextent_replica_start (next);
  519.            end = Fextent_replica_end (next);
  520.  
  521.            Fset_extent_endpoints (extent_obj, start, end);
  522.          }
  523.           else
  524.         {
  525.         rotten:
  526.           signal_simple_continuable_error
  527.         ("Something rotten in the state of undo:", next);
  528.         }
  529.         }
  530.       arg--;
  531.     }
  532.  
  533.   UNGCPRO;
  534.   return unbind_to (speccount, list);
  535. }
  536.  
  537. void
  538. syms_of_undo (void)
  539. {
  540.   defsubr (&Sprimitive_undo);
  541.   defsubr (&Sundo_boundary);
  542.   defsymbol (&Qinhibit_read_only, "inhibit-read-only");
  543. }
  544.  
  545. void
  546. vars_of_undo (void)
  547. {
  548.   inside_undo = 0;
  549.   pending_boundary = Qnil;
  550.   staticpro (&pending_boundary);
  551.   last_undo_buffer = Qnil;
  552.   staticpro (&last_undo_buffer);
  553. }
  554.